package com.example.ioc.factory;

import java.io.IOException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class BeanFactory {

    private static Map<String, Object> map=new HashMap<>();

    static {
        Properties properties=new Properties();
        try {
            properties.load(BeanFactory.class.getClassLoader().getResourceAsStream("db.properties"));
            Enumeration<?> enumeration = properties.propertyNames();
            while (enumeration.hasMoreElements()){
                String key = (String) enumeration.nextElement();
                String value= (String) properties.get(key);
                Object obj = Class.forName(value).newInstance();

                //****aop**********
                BeanProxyFactory beanProxyFactory=new BeanProxyFactory(obj);
                Object bean = beanProxyFactory.getBean();
                //****aop**********
                map.put(key,bean);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Object getBean(String name){
        return map.get(name);
    }
}
